package com.dllo.lanou.cookapp.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.dllo.lanou.cookapp.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.SaveListener;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    @Bind(R.id.et__reg_password)
    EditText et_reg_password;
    @Bind(R.id.et_reg_username)
    EditText et_reg_username;
    @Bind(R.id.et_reg_email)
    EditText et_reg_email;
    @Bind(R.id.bt_register)
    Button bt_register;
    @Bind(R.id.iv_reg_back)
    ImageView iv_reg_back;
    private String s_username, s_pwd, s_email;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        bt_register.setOnClickListener(this);
        iv_reg_back.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_register:
                s_username = et_reg_username.getText().toString();
                s_pwd = et_reg_password.getText().toString();
                s_email = et_reg_email.getText().toString();


                //注册操作
                BmobUser bu = new BmobUser();
                bu.setUsername(s_username);
                bu.setEmail(s_email);
                bu.setPassword(s_pwd);

                bu.signUp(this, new SaveListener() {
                    @Override
                    public void onSuccess() {
                        //注册成功
                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onFailure(int i, String s) {

                    }
                });

                Intent intent = new Intent();
                intent.putExtra("username", s_username);
                intent.putExtra("pwd", s_pwd);
                setResult(1, intent);
                finish();
                break;

            case R.id.iv_reg_back:
                break;
        }

    }
}
